// pages/search/index.js
import {request} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    isFocus:true,
    inputVal:""
  },
  timeId:-1,

  //输入框搜索
  handleInput(e){
    console.log(e);
    let {value}=e.detail;

    if(!value.trim()){
      this.setData({
        goods:[],
        isFocus:true
      })
      clearTimeout(this.timeId);
      return;
    }
    //发送请求
    this.setData({
      isFocus:false
    })
    clearTimeout(this.timeId);
    this.timeId = setTimeout(()=>{
      this.querySearch(value);
    },1000)
  },

  async querySearch(query){
    let res= await request({url:"/goods/qsearch",data:{query}});
    console.log(res);
    this.setData({
      goods:res
    })
  },

  handleCancel(){

    this.setData({
      goods:[],
      isFocus:true,
      inputVal:""
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

})